package cn.itcast.item.web;


import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.item.service.Impl.ItemServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private ItemServiceImpl itemService;

    /**
     * 分页查询商品
     * @param param
     * @return PageDTO
     */
    @PostMapping("list")
    public PageDTO<Item> findItemByPage(@RequestBody SearchItemDTO param) {
        LambdaQueryWrapper<Item> wrap = null;
        String name = param.getName();
        Date beginDate = param.getBeginDate();
        Date endDate = param.getEndDate();
        String brand = param.getBrand();
        String category = param.getCategory();

        if (StringUtils.isNotBlank(name)) {
            wrap = Wrappers.lambdaQuery();
            wrap.like(Item::getName, name);
        }
        if (StringUtils.isNotBlank(brand)) {
            if (wrap == null) {
                wrap = Wrappers.lambdaQuery();
            }
            wrap.eq(Item::getBrand, brand);
        }
        if (beginDate != null) {
            if (wrap == null) {
                wrap = Wrappers.lambdaQuery();
            }
            wrap.ge(Item::getCreateTime, beginDate);
        }
        if (endDate != null) {
            if (wrap == null) {
                wrap = Wrappers.lambdaQuery();
            }
            wrap.le(Item::getCreateTime, endDate);
        }

        if (StringUtils.isNotBlank(category)) {
            if (wrap == null) {
                wrap = Wrappers.lambdaQuery();
            }
            wrap.eq(Item::getCategory, category);
        }
        Page<Item> page = new Page<>(param.getPage(), param.getSize());
        itemService.page(page, wrap);
        return new PageDTO<>(page.getTotal(), page.getRecords());
    }

    /**
     * 根据Id查询商品
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Item findItemById(@PathVariable Long id){
        Item item = itemService.getById(id);
        return item;
    }

    /**
     * 新增商品
     * @param item
     * @return
     */
    @PostMapping("")
    public ResultDTO addItem(@RequestBody Item item){
        item.setCreateTime(new Date());
        boolean b = itemService.save(item);
        ResultDTO dto = new ResultDTO();
        dto.setSuccess(b);
        return dto;
    }

    /**
     * 修改商品状态，上下架
     * @param id
     * @return
     */
    @PutMapping("status/{id}/{status}")
    public ResultDTO statusPut(@PathVariable Long id,@PathVariable Integer status){
        Item item = itemService.getById(id);
        item.setStatus(status);
        boolean b = itemService.updateById(item);
        ResultDTO dto = new ResultDTO();
        dto.setSuccess(b);
        return dto;
    }

    /**
     * 修改商品信息
     * @param item
     * @return
     */
    @PutMapping("")
    public ResultDTO updateItem(@RequestBody Item item){
        item.setUpdateTime(new Date());
        boolean b = itemService.updateById(item);
        ResultDTO dto = new ResultDTO();
        dto.setSuccess(b);
        return dto;
    }

    /**
     * 根据ID删除商品
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResultDTO del(@PathVariable String id) {
        boolean b = itemService.removeById(id);
        ResultDTO dto = new ResultDTO();
        dto.setSuccess(b);
        return dto;
    }













}